<?php 
class ControllerAccountCreate extends Controller {
	var $error = array();
	      
  	function index() {
		$cache    =& $this->locator->get('cache');
		$cart     =& $this->locator->get('cart');
		$config   =& $this->locator->get('config');
		$customer =& $this->locator->get('customer');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		    
		if ($customer->isLogged()) {
	  		$response->redirect($url->ssl('account'));
    	}

    	$language->load('controller/account_create.php');

    	$template->set('title', $language->get('heading_title'));
	
    	if ($request->isPost() && $request->has('firstname', 'post') && $this->validate()) {
      		$sql = "insert into customer set firstname = '?', lastname = '?', email = '?', telephone = '?', fax = '?', password = '?', newsletter = '?', status = '1', date_added = now()";
      		$database->query($database->parse($sql, $request->gethtml('firstname', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('email', 'post'), $request->gethtml('telephone', 'post'), $request->gethtml('fax', 'post'), md5($request->gethtml('password', 'post')), $request->gethtml('newsletter', 'post')));

      		$customer->login($request->gethtml('email', 'post'), $request->gethtml('password', 'post'));

      		$sql = "insert into address set customer_id = '?', firstname = '?', lastname = '?', company = '?', address_1 = '?', address_2 = '?', city = '?', postcode = '?', country_id = '?', zone_id = '?'";
      		$database->query($database->parse($sql, $customer->getId(), $request->gethtml('firstname', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('company', 'post'), $request->gethtml('address_1', 'post'), $request->gethtml('address_2', 'post'), $request->gethtml('city', 'post'), $request->gethtml('postcode', 'post'), $request->gethtml('country_id', 'post'), $request->gethtml('zone_id', 'post')));

      		$database->query("update customer set address_id = '" . (int)$database->getLastId() . "' where customer_id = '" . (int)$customer->getId() . "'");
			
			$mail = $this->locator->create('mail');
      		
			$mail->setTo($request->gethtml('email', 'post'));
	  		$mail->setFrom($config->get('config_email'));
	  		$mail->setSender($config->get('config_store'));
	  		$mail->setSubject($language->get('email_subject', $config->get('config_store')));
	  		$mail->setCharacterSet($language->get('charset'));
	  		$mail->setText($language->get('email_message', $request->gethtml('firstname', 'post'), $config->get('config_store'), $url->ssl('account_login'), $config->get('config_store')));
      		$mail->send();
			if ($config->has('config_email_send_account_create') && $config->get('config_email_send_account_create')) {
				$mail->setTo($config->get('config_email'));
				$mail->send();
			}
	  	  
	  		$response->redirect($url->ssl('account_success'));
    	} 
 
    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

		$view->set('text_yes', $language->get('text_yes'));
		$view->set('text_no', $language->get('text_no'));
    	$view->set('text_account_already', $language->get('text_account_already', $url->ssl('account_login')));
    	$view->set('text_your_details', $language->get('text_your_details'));
    	$view->set('text_your_address', $language->get('text_your_address'));
    	$view->set('text_your_password', $language->get('text_your_password'));
		$view->set('text_newsletter', $language->get('text_newsletter'));
				
    	$view->set('entry_firstname', $language->get('entry_firstname'));
    	$view->set('entry_lastname', $language->get('entry_lastname'));
    	$view->set('entry_email', $language->get('entry_email'));
    	$view->set('entry_telephone', $language->get('entry_telephone'));
    	$view->set('entry_fax', $language->get('entry_fax'));
    	$view->set('entry_company', $language->get('entry_company'));
    	$view->set('entry_address_1', $language->get('entry_address_1'));
    	$view->set('entry_address_2', $language->get('entry_address_2'));
    	$view->set('entry_postcode', $language->get('entry_postcode'));
    	$view->set('entry_city', $language->get('entry_city'));
    	$view->set('entry_country', $language->get('entry_country'));
    	$view->set('entry_zone', $language->get('entry_zone'));
		$view->set('entry_newsletter', $language->get('entry_newsletter'));
    	$view->set('entry_password', $language->get('entry_password'));
    	$view->set('entry_confirm', $language->get('entry_confirm'));

		$view->set('button_continue', $language->get('button_continue'));
    
		$view->set('error', @$this->error['message']);
		$view->set('error_firstname', @$this->error['firstname']);
    	$view->set('error_lastname', @$this->error['lastname']);
    	$view->set('error_email', @$this->error['email']);
    	$view->set('error_telephone', @$this->error['telephone']);
    	$view->set('error_password', @$this->error['password']);
    	$view->set('error_confirm', @$this->error['confirm']);
    	$view->set('error_address_1', @$this->error['address_1']);
    	$view->set('error_city', @$this->error['city']);
    	$view->set('error_postcode', @$this->error['postcode']);

    	$view->set('action', $url->ssl('account_create'));

    	$view->set('firstname', $request->gethtml('firstname', 'post'));

    	$view->set('lastname', $request->gethtml('lastname', 'post'));

    	$view->set('email', $request->gethtml('email', 'post'));

    	$view->set('telephone', $request->gethtml('telephone', 'post'));

    	$view->set('fax', $request->gethtml('fax', 'post'));

    	$view->set('company', $request->gethtml('company', 'post'));

    	$view->set('address_1', $request->gethtml('address_1', 'post'));

    	$view->set('address_2', $request->gethtml('address_2', 'post'));

    	$view->set('postcode', $request->gethtml('postcode', 'post'));

    	$view->set('city', $request->gethtml('city', 'post'));

      	$view->set('agreed', $request->gethtml('agreed', 'post'));

    	$view->set('countries', $database->cache('country', "select * from country where status = '1' order by name"));

    	$view->set('zones', $database->cache('zone', "select * from zone order by country_id, name"));

    	if ($request->has('country_id', 'post')) {
      		$view->set('country_id', $request->gethtml('country_id', 'post'));
		} else {
      		$view->set('country_id', $config->get('config_country_id'));
    	}

    	if ($request->has('zone_id', 'post')) {
      		$view->set('zone_id', $request->gethtml('zone_id', 'post'));
    	} else {
		//	$view->set('zone_id', $config->get('config_zone_id'));
			$def_zone = reset($view->data['zones']);
      		$view->set('zone_id', $def_zone['zone_id']);
    	}
    	
    	$view->set('password', $request->gethtml('password', 'post'));

    	$view->set('confirm', $request->gethtml('confirm', 'post'));
		
		$view->set('newsletter', $request->gethtml('newsletter', 'post'));

		if ($config->get('config_account_id')) {
			$information_info = $database->getRow("select * from information i left join information_description id on (i.information_id = id.information_id) where i.information_id = '" . (int)$config->get('config_account_id') . "'");
		
			$view->set('agree', $language->get('text_agree', $url->href('information', FALSE, array('information_id' => $config->get('config_account_id'))), $information_info['title']));
		}
						
		$template->set('content', $view->fetch('content/account_create.tpl'));
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));	
  	}

  	function validate() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$validate =& $this->locator->get('validate');

		if (!$validate->strlen($request->gethtml('firstname', 'post'),1,32)) {
      		$this->error['firstname'] = $language->get('error_firstname');
    	}

		if (!$validate->strlen($request->gethtml('lastname', 'post'),1,32)) {
      		$this->error['lastname'] = $language->get('error_lastname');
    	}

    	if ((!$validate->strlen($request->gethtml('email', 'post'), 1, 32)) || (!$validate->email($request->gethtml('email', 'post')))) {
      		$this->error['email'] = $language->get('error_email');
    	}

    	if ($database->getRow($database->parse("select * from customer where email = '?'", $request->gethtml('email', 'post')))) {
      		$this->error['message'] = $language->get('error_exists');
    	}

		if (!$validate->strlen($request->gethtml('password', 'post'),4,20)) {
      		$this->error['password'] = $language->get('error_password');
    	}

    	if ($request->gethtml('confirm', 'post') != $request->gethtml('password', 'post')) {
      		$this->error['confirm'] = $language->get('error_confirm');
    	}

		if (!$validate->strlen($request->gethtml('address_1', 'post'),3,64)) {
      		$this->error['address_1'] = $language->get('error_address_1');
    	}

		if (!$validate->strlen($request->gethtml('city', 'post'),3,32)) {
      		$this->error['city'] = $language->get('error_city');
    	}
    	
    	if (!$validate->strlen($request->gethtml('postcode', 'post'),3,32)) {
      		$this->error['postcode'] = $language->get('error_postcode');
    	}

		if (!$validate->strlen($request->gethtml('telephone', 'post'),3,32)) {
      		$this->error['telephone'] = $language->get('error_telephone');
    	}

    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}
  	}
  
  	function zone() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		
    	$output = '<select name="zone_id">';

    	$results = $database->cache('zone-' . (int)$request->gethtml('country_id'), "select zone_id, name from zone where country_id = '" . (int)$request->gethtml('country_id') . "' order by name");
        
      	foreach ($results as $result) {
        	$output .= '<option value="' . $result['zone_id'] . '"';
	
	    	if ($request->gethtml('zone_id') == $result['zone_id']) {
	      		$output .= ' SELECTED';
	    	}
	
	    	$output .= '>' . $result['name'] . '</option>';
    	} 
		
		if (!$results) {
		  	$output .= '<option value="0">' . $language->get('text_none') . '</option>';
    	}

    	$output .= '</select>';
	
		$response->set($output);
  	}  
}
?>
